﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using MAT.Model;
using System.Web.Security;

namespace MAT.Controllers
{
    class ctrManageUser
    {
        private frmManageUser _frmManageUser = null;

        public frmManageUser FrmManageUser
        {
            get { return _frmManageUser; }
            set { _frmManageUser = value; }
        }

        MAT_Context context = MATContext.getContext();

        private bool check()
        {
            if (_frmManageUser.txtUsername.Text == "")
            {
                MessageBox.Show("Name is empty");
                _frmManageUser.txtUsername.Focus();
                return false;
            }
            else
                return true;
        }

        private int checkPriority(string input)
        {
            int output = 0;
            switch (input)
            {
                case "Admin_0": output = 0; break;
                case "Admin_1": output = 1; break;
                case "Admin_2": output = 2; break;
                case "User": output = 3; break;
            }
            return output;
        }

        public void frmManageUser_Load()
        {
            try
            {
                _frmManageUser.cmbEmployeeName.DataSource = context.OWNERS.ToList();
                _frmManageUser.cmbEmployeeName.DisplayMember = "EMPLOYEE_NAME";
                _frmManageUser.cmbEmployeeName.ValueMember = "EMPLOYEE_ID";
                _frmManageUser.txtEmployeeID.Text = _frmManageUser.cmbEmployeeName.SelectedValue.ToString();

                _frmManageUser.txtUsername.GotFocus += new EventHandler(txtUsername_GotFocus);
                _frmManageUser.txtUsername.MouseClick += new MouseEventHandler(txtUsername_GotFocus);
            }
            catch { }
        }

        public void btnAdd_Click()
        {
            if (_frmManageUser.btnAdd.Text == "Add")
            {
                if (check())
                {
                    if (context.LOG_IN.FirstOrDefault(log => log.USERNAME.ToLower() == _frmManageUser.txtUsername.Text.ToLower()) == null)
                    {
                        LOG_IN log = new LOG_IN();
                        log.EMPLOYEE_ID = _frmManageUser.txtEmployeeID.Text;
                        log.USERNAME = _frmManageUser.txtUsername.Text;
                        log.PASSWORDS = FormsAuthentication.HashPasswordForStoringInConfigFile("jupiter", "MD5");
                        log.PRIORITY = checkPriority(_frmManageUser.cmbPriority.SelectedItem.ToString());
                        try
                        {
                            context.LOG_IN.AddObject(log);
                            context.SaveChanges();
                            MessageBox.Show("SUCCESSFUL!");
                            frmManageUser_Load();
                        }
                        catch { }

                        _frmManageUser.btnReset.Visible = false;
                        _frmManageUser.btnEdit.Enabled = true;
                        _frmManageUser.btnDelete.Enabled = true;
                        _frmManageUser.cmbEmployeeName.Enabled = true;
                        _frmManageUser.txtUsername.ReadOnly = true;
                        _frmManageUser.btnAdd.Text = "New";
                        _frmManageUser.btnClose.Text = "Close";
                        _frmManageUser.cmbPriority.Visible = false;
                        return;
                    }
                    else
                        MessageBox.Show("User name is exist!");
                }
            }
            if (_frmManageUser.btnAdd.Text == "New")
            {
                _frmManageUser.btnReset.Visible = true;
                _frmManageUser.btnEdit.Enabled = false;
                _frmManageUser.btnDelete.Enabled = false;
                _frmManageUser.cmbEmployeeName.Enabled = false;
                _frmManageUser.txtUsername.ReadOnly = false;
                _frmManageUser.btnAdd.Text = "Add";
                _frmManageUser.btnClose.Text = "Cancel";
                _frmManageUser.cmbPriority.Visible = true;
                _frmManageUser.cmbPriority.SelectedItem = "User";
                return;
            }
        }

        public void btnDelete_Click()
        {
            LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.EMPLOYEE_ID == _frmManageUser.txtEmployeeID.Text);
            if (login != null)
            {
                try
                {
                    context.LOG_IN.DeleteObject(login);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    frmManageUser_Load();
                }
                catch { }
            }
            else
                MessageBox.Show("Not Found!");
        }

        public void btnEdit_Click()
        {
            LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.EMPLOYEE_ID == _frmManageUser.txtEmployeeID.Text);
            if (login != null)
            {
                try
                {
                    login.PRIORITY = checkPriority(_frmManageUser.cmbPriority.SelectedItem.ToString());
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    frmManageUser_Load();
                }
                catch { }
            }
            else
                MessageBox.Show("Not Found!");
        }

        public void btnReset_Click()
        {
            _frmManageUser.txtUsername.Text = "";
            _frmManageUser.cmbPriority.SelectedItem = "User";
        }

        public void btnClose_Click()
        {
            if (_frmManageUser.btnClose.Text == "Close")
            {
                _frmManageUser.Close();
                MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
            }
            if (_frmManageUser.btnClose.Text == "Cancel")
            {
                _frmManageUser.btnReset.Visible = false;
                _frmManageUser.btnEdit.Enabled = true;
                _frmManageUser.btnDelete.Enabled = true;
                _frmManageUser.cmbEmployeeName.Enabled = true;
                _frmManageUser.txtUsername.ReadOnly = true;
                _frmManageUser.btnAdd.Text = "New";
                _frmManageUser.btnClose.Text = "Close";
                _frmManageUser.cmbPriority.Visible = false;
            }
        }

        public void cmbEmployeeName_SelectedIndexChanged()
        {
            try
            {
                string a = _frmManageUser.cmbEmployeeName.SelectedValue.ToString();
                _frmManageUser.txtEmployeeID.Text = a;
                LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.EMPLOYEE_ID == a);
                if (login != null)
                {
                    _frmManageUser.txtUsername.Text = login.USERNAME;
                    _frmManageUser.cmbPriority.Visible = true;
                    switch (login.PRIORITY.ToString())
                    {
                        case "0": _frmManageUser.cmbPriority.SelectedItem = "Admin_0"; break;
                        case "1": _frmManageUser.cmbPriority.SelectedItem = "Admin_1"; break;
                        case "2": _frmManageUser.cmbPriority.SelectedItem = "Admin_2"; break;
                        case "3": _frmManageUser.cmbPriority.SelectedItem = "User"; break;
                    }
                }
                else
                {
                    _frmManageUser.txtUsername.Text = "";
                    _frmManageUser.cmbPriority.Visible = false;
                }
            }
            catch { }
        }

        public void txtUsername_TextChanged()
        {
            if (_frmManageUser.btnAdd.Text == "New")
            {
                if (_frmManageUser.txtUsername.Text == "")
                    _frmManageUser.btnAdd.Enabled = true;
                else
                    _frmManageUser.btnAdd.Enabled = false;
            }
        }

        public void txtUsername_KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnAdd_Click();
        }

        void txtUsername_GotFocus(object sender, EventArgs e)
        {
            _frmManageUser.txtUsername.SelectAll();
        }
    } 
}
